# coding:utf-8
import os

from flask import Flask
# from flask_apscheduler import APScheduler

def register_blueprint(app):
    from app.api.v1 import create_blueprint
    app.register_blueprint(create_blueprint(), url_prefix='/v1')


def register_plugin(app):
    from app.models.base import db
    # initialize the app with the extension
    db.init_app(app)
    with app.app_context():
        db.create_all()

    # 任务调度管理
    # scheduler = APScheduler()
    # scheduler.init_app(app)
    # scheduler.start()


def create_app():
    app = Flask(__name__)
    app.config.from_object('app.config.base_setting')
    # 根据环境加载变量
    if "ops_config" in os.environ:
        app.config.from_object('config/%s_setting.py' % os.environ['ops_config'])
    # 注册蓝图
    register_blueprint(app)
    # 注册插件
    register_plugin(app)
    return app
